<template>
  <div class='login'>
    <div class='login-box'>
      <el-form ref="ref_form" :rules="rules" :model="fromData" >
        <el-form-item prop='zh'>
          <el-input  prefix-icon="el-icon-user" v-model='fromData.zh' ></el-input>
        </el-form-item>
        <el-form-item prop='mm'>
          <el-input type='password' prefix-icon="el-icon-lock" v-model='fromData.mm'></el-input>
        </el-form-item>
      </el-form>
      <div class='login-button'>
        <el-button type="primary" @click='login' >登陆</el-button>
        <el-button type="success" @click='reset'>重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return {
      fromData:{
        zh:'',
        mm:''
      },
      rules:{
        zh: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
        mm: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
          ],
      }
    }
  },
  methods:{
    login(){
      this.$refs.ref_form.validate(async res => {
        if( res ) {
          let { data:data } = await this.axios.post('/login', { username:this.fromData.zh, password:this.fromData.mm })
          if(data.meta.status == 200) {
            window.sessionStorage.setItem('token',data.data.token)
            this.$router.push('/home')
            this.pop.success('登录成功！！')
          } else
            this.pop.error('登录失败')
        }
      })
    },
    reset(){
      this.$refs.ref_form.resetFields()
    }
  }
}
</script>

<style lang='scss' scoped>
.login {
  width:100%;
  height:100%;
  background-color:rgb(19, 73, 124);
  position:relative;
  overflow: hidden;
}

.login .login-box {
  width:400px;
  height:300px;
  background-color:white;
  border-radius:15px;
  position:absolute;
  left:50%;
  top:50%;
  transform: translateX(-50%) translateY(-50%);
  padding:0 40px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login .login-box .login-button {
  display:flex;
  justify-content: space-around;
  margin-top:20px;
  .el-button {
    flex-basis: 150px;
  }
}


</style>